package com.example.fido2013;

import java.net.URI;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.widget.RadioGroup;
import android.app.ProgressDialog;
import android.content.Context;

public class APICallDogVestsAsync extends AsyncTask<String, Integer, JSONObject> {

	String sessionName;
	String sessionId;
	int vestID;

	public APICallDogVestsAsync(String sessionName, String sessionId, int vestID) {
		this.sessionName = sessionName;
		this.sessionId = sessionId;
		this.vestID = vestID;
	}

	protected void onPreExecute() {
		super.onPreExecute();
	}

	protected JSONObject doInBackground(String... params) {
		String apiLocation = "http://dev.m.gatech.edu/d/vkrishnan35/w/fidocpy/c/api/updatevestslocation/" + vestID;
		JSONObject jsonObject = null;

		try {
			URI api = new URI(apiLocation);
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(api);

			request.setHeader("Cookie", sessionName + "=" + sessionId);

			HttpResponse response = client.execute(request);
			HttpEntity entity = response.getEntity();
			String str = EntityUtils.toString(entity);

			try {
				jsonObject = new JSONObject(str);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonObject;
	}

	protected void onPostExecute(JSONObject result) {
		super.onPostExecute(result);
		Message message = new Message();
		handler.sendMessage(message);
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
//			progressDialog.dismiss();
		}
	};

	protected void onCancelled() {
		super.onCancelled();
	}
}
